<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Code here is run before frontend controllers
 *
 * @author      PyroCMS Dev Team
 * @copyright   Copyright (c) 2012, PyroCMS LLC
 * @package 	PyroCMS\Core\Controllers
 */

class Public_Controller extends MY_Controller
{
	
	/**
	 * Loads the gazillion of stuff, in Flash Gordon speed.
	 * @todo Document properly please.
	 */
	public $config_module;
	public $reset_layout = FALSE;
	public $user_profile;
	public $action = array();
	public $role_full = false;
	public $name_employee = 'employee'; // table : ser_groups lấy name của nhân viên 
	public function __construct()
	{
		parent::__construct();
		
		
		$this->benchmark->mark('public_controller_start');
		if($this->router->fetch_class()!='news'){
			if(!$this->current_user){
				if($this->module != 'users'){
					redirect('users/login','refresh');
				}
				else{
					if($this->method == 'index')
						redirect('users/login','refresh');
				}
			}
		}
		$this->config->set_item('language',$this->lang_current) ;
		
		$this->load->language('users/users',$this->lang_current);
		// Check redirects if GET and Not AJAX
		/*if ( ! $this->input->is_ajax_request() and $_SERVER['REQUEST_METHOD'] == 'GET')
		{
			$this->load->model('redirects/redirect_m');
			$uri = trim(uri_string(), '/');

			if ($uri and $redirect = $this->redirect_m->get_from($uri))
			{
				// Check if it was direct match
				if ($redirect->from == $uri)
				{
					redirect($redirect->to, 'location', $redirect->type);
				}

				// If it has back reference
				if (strpos($redirect->to, '$') !== false)
				{
					$from = str_replace('%', '(.*?)', $redirect->from);
					$redirect->to = preg_replace('#^'.$from.'$#', $redirect->to, $uri);
				}
				// Redirect with wanted redirect header type
				redirect($redirect->to, 'location', $redirect->type);
			}
		}

		Events::trigger('public_controller');

		// Check the frontend hasnt been disabled by an admin
		if ( ! $this->settings->frontend_enabled && (empty($this->current_user) or $this->current_user->group != 'admin'))
		{
			header('Retry-After: 600');

			$error = $this->settings->unavailable_message ? $this->settings->unavailable_message : lang('cms:fatal_error');
			show_error($error, 503);
		}
*/


/*
		// -- Navigation menu -----------------------------------
		$this->load->model('pages/page_m');

		// Load the current theme so we can set the assets right away
		ci()->theme = $this->theme_m->get();

		if (empty($this->theme->slug))
		{
			show_error('This site has been set to use a theme that does not exist. If you are an administrator please '.anchor('admin/themes', 'change the theme').'.');
		}

		// Set the theme as a path for Asset library
		Asset::add_path('theme', $this->theme->path.'/');
		Asset::set_path('theme');

		// Support CDN URL's like Amazon CloudFront 
		if (Settings::get('cdn_domain'))
		{
			$protocol = ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ? 'https' : 'http';

			// Make cdn.pyrocms.com into https://cdn.pyrocms.com/
			Asset::set_url($protocol.'://'.rtrim(Settings::get('cdn_domain'), '/').'/');
		}

		// Set the theme view folder
*/
		//$theme_path = 'themes/default';
		//Asset::add_path('theme', $theme_path.'/');
		//Asset::set_path('theme');
		$this->template->set_theme('default');
		//print_r($this->template->get_layouts());exit;
		if ($this->template->layout_exists($this->module.'.html'))
		{
//				echo 1;exit;
			$this->template->set_layout($this->module.'.html');
		}

		// Nope, just use the default layout
		elseif ($this->template->layout_exists('default.html'))
		{
			$this->template->set_layout('default.html');
		}
		
		if($this->module){
			//load base controller cho module
			include_once(APPPATH.'modules/base.php');
			$base = new base($this->module);
			$this->config_module = $base->config_module;

		}
		$this->template->set_metadata('canonical', site_url($this->uri->uri_string()), 'link');
		$this->template->set('error',$this->session->flashdata('error'));
		//$this->template->set('success',$this->session->flashdata('success'));
		

		
		$this->template->server = $_SERVER;
		$this->load->library('kunkun');
		
		$this->load->helper('menu');
		//menu_active();
		
		$this->load->language('global',$this->lang_current);
		
		$this->load->library('kunkuncache');
		//luu file cache profile
		$this->load->helper('profile');
		list_profile();
		
		if($this->current_user){
			if(!$this->current_profile && $this->current_user->id != 1){
			$this->session->set_userdata('current_user', NULL);
			$this->session->set_flashdata('error',lang('global:no_employee'));
			//$this->session->set_userdata('error',lang('global:no_employee'));
			redirect('/users/login','refresh');
			}
			$this->template->set('user',$this->current_user);
			$this->template->set('current_profile',$this->current_profile);
			
			//ss
			if(!empty($this->current_profile->background)){
				$bg = $this->session->userdata('background');
				if(empty($bg))
					$this->session->set_userdata('background',$this->current_profile->background);
				$this->template->set('background',$this->session->userdata('background'));
				//echo $this->session->userdata('background');
			}
		}
		//echo $_SERVER['HTTP_USER_AGENT'];exit;
		if(!empty($this->current_user)){
			if ($this->current_user->group === 'admin')
			{
				$this->role_full = true;
				$this->template->set('is_admin',true);
				//$this->template->set('admin',true)->append_css('toolbar.css');
			}
			//giam doc
			if($this->current_user->group === 'boss1' || $this->current_user->group === 'boss11'){
				
				$this->role_full = true;
				$this->template->set('role_full',true);
			}
		}
		$this->benchmark->mark('public_controller_end');
		
	
	}
	public function check_permissions(){
		if($this->current_user->group_id != 1){
				$this->session->set_flashdata('error', lang('global:access-denied'));
				redirect("/",'refresh');
		}
	}
	//lay gio he thong de khi di nuoc ngoai
	public function time_zone(){
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$result = array("date"=>date('d/m/Y'),'a'=>date('a'),'d'=>date('d'),'m'=>date('m'),'Y'=>date('Y'));
		return $result;
	}
}
